package com.Travit;

import java.io.IOException;
import java.util.Hashtable;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.Travit.TravITActivity.clicker;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

public class Distributor extends Activity{

	
	ImageButton intermediate;
	ImageButton logoutbutton;
	ImageButton shippingButton;
	private String loggedid;
	private String loginname;
	private static String scannedItem="";
	private static View disView;
	private boolean scanned=false;
	private static String shippingOrRecieving="none";
	private static String urlRequestValidateBoxBarcode = "http://project492.engr.sjsu.edu/travITvalidateBox.php";
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		Log.i("login", "time");
		Bundle extras = getIntent().getExtras();
		loggedid = extras.getString("loggedid");
		loginname = extras.getString("loginname");
		Log.v("in dist", loginname);
		setContentView(R.layout.distributorhome);
		setTitle(R.string.disthome);
		TextView name = (TextView) findViewById(R.id.distributorlogintext);
		name.setText("Welcome " + loginname.toString() + "");
		logoutbutton = (ImageButton) findViewById(R.id.logout);
		intermediate = (ImageButton) findViewById(R.id.intermediate);
		shippingButton = (ImageButton) findViewById(R.id.finalstop);
		
		
		logoutbutton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.v("starting", "recieving final");
				disView=v;
				Intent newIntent = new Intent(v.getContext(), TravITActivity.class);
				newIntent.putExtra("venid", loggedid);
				Log.i("logout", "distributor");
				startActivity(newIntent);
			}			
		});
		intermediate.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				 Intent intent = new Intent("com.google.zxing.client.android.SCAN");
				 intent.setPackage("com.google.zxing.client.android");
			     intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "QR_CODE_MODE");
			     shippingOrRecieving="recieving";
			     startActivityForResult(intent, 0);
			}
/*
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String responseResult = null;
				String boxBarCodeID = "1000";
				HttpClient client = new DefaultHttpClient();
				HttpGet request = new HttpGet(urlRequestValidateBoxBarcode
						+ "?boxbarcodeID="+boxBarCodeID);
				try {
					HttpResponse response = client.execute(request);
					HttpEntity entity = response.getEntity();
					responseResult = EntityUtils.toString(entity);
					Log.v("validInvalid", "" + responseResult);
					if(responseResult.equalsIgnoreCase("Valid Box"))
					{
						Intent newIntent = new Intent(v.getContext(), DistributorIntermediate.class);
						newIntent.putExtra("venid", loggedid);
						startActivity(newIntent);
					}
					else
					{
						Intent newIntent = new Intent(v.getContext(), DistributorIntermediate.class);
						newIntent.putExtra("venid", loggedid);
						startActivity(newIntent);
					}
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				}
				if(responseResult.equalsIgnoreCase("Valid Box"))
				{
					Intent newIntent = new Intent(v.getContext(), DistributorIntermediate.class);
					newIntent.putExtra("venid", loggedid);
					startActivity(newIntent);
				}
			}
			*/			
		});
		
		shippingButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				 Intent intent = new Intent("com.google.zxing.client.android.SCAN");
			     intent.setPackage("com.google.zxing.client.android");
			     intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "QR_CODE_MODE");
			     shippingOrRecieving="shipping";
			     startActivityForResult(intent, 0);
				
			}			
		});

	}
	
	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		
	    if (requestCode == 0) {
	        if (resultCode == RESULT_OK) {
	            String contents = intent.getStringExtra("SCAN_RESULT");
	            String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
	            // Handle successful scan
	            Log.v("scanned propoerly", "yes");
	            Log.v("intent",""+intent);
	            Log.v("scanned item is:",contents);
	            TravITActivity t=new TravITActivity();
	            String[]temp=contents.split("/-");
	            
	         //   clicker click=t.new clicker();
	           // click.createMessage("contents"+contents);
	            
	            HttpGet request = new HttpGet(urlRequestValidateBoxBarcode
	    				+ "?boxbarcodeID="+contents);
	    		HttpClient client = new DefaultHttpClient();
	    		Hashtable<String,String>table;
	    		try{
	    			HttpResponse response=client.execute(request);
	    			HttpEntity entity = response.getEntity();
	    			Log.v("entity:",entity.toString());
	    			String responseResult = EntityUtils.toString(entity);
	    			Log.v("Box Results::",responseResult);
	    			
	    			Log.v("hahahahhahaha","hahahahhaha");
	    			String[] responseResultArr=responseResult.split(";");
	    			responseResultArr[0]=responseResultArr[0].trim();
	    			table=new Hashtable<String,String>();
	    			for(int i=0;i<responseResultArr.length;i+=2){
	    				Log.v("response:",responseResultArr[i]);
	    				int tempVar=i;
	    				tempVar++;
	    				Log.v("result:",responseResultArr[tempVar]);
	    				table.put(responseResultArr[i],responseResultArr[tempVar]);
	    			}

	    			if(!table.get("Status").equals("Invalid Box")){
	    		           scanned=true;
	    		           scannedItem=contents;
	    		           Intent newIntent = new Intent(intermediate.getContext(), RecieveShipment.class);
	    		           newIntent.putExtra("scannedbarcode", contents);
	    		           newIntent.putExtra("loginname", loginname);
	    		           newIntent.putExtra("loggedid", loggedid);
	    		           newIntent.putExtra("scannedBARCODEID", contents);
	    		           newIntent.putExtra("Table", responseResultArr);
	    		           newIntent.putExtra("BoxBarCodeID", contents);
	    		           if(shippingOrRecieving.equals("shipping"))
	    		        	   newIntent.putExtra("status", "shipped");
	    		           else
	    		        	   newIntent.putExtra("status", "recieved");
	    		           newIntent.putExtra("updatedByType", "Distributor");
	    		           startActivity(newIntent);
	    		    }
	    			
	    		}
	    		catch (Exception e) {
	    			// TODO Auto-generated catch block
	    			e.printStackTrace();
	    		}
	    		
	            
	        } else if (resultCode == RESULT_CANCELED) {
	            // Handle cancel
	        }
	    }
	}
	
}
